package com.ylbh.modules.activity.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.activity.dto.BatchPurchaseGoodsDTO;
import com.ylbh.common.pojo.activity.dto.GoodsListDTO;
import com.ylbh.common.pojo.goods.dto.GoodsClassDTO;
import com.ylbh.common.pojo.goods.dto.SubGoodsClassDTO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.activity.service.ActivityGoodsService;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 添加活动商品时 需要的商品信息
 *
 * @author mgh
 * @date 2019-04-22 15:25:57
 */
@RestController
@RequestMapping("/activity/goods")
public class ActivityGoodsController {

    @Autowired
    private ActivityGoodsService activityGoodsService;
    /**
     * 获取分类列表
     */
    @RequestMapping("/queryClassList")
    public CommonResult<List<GoodsClassDTO>> classList(){
        List<GoodsClassDTO> goodsClasses = activityGoodsService.queryClassList();

        return Ref.ok(goodsClasses);
    }

    /**
     * 获取商品列表
     */
    @RequestMapping("/queryGoodsList")
    public CommonResult<PageUtils> goodsList( GoodsListDTO goodsListDTO){
        PageUtils page = activityGoodsService.queryGoodsList(goodsListDTO);

        return Ref.ok(page);
    }

    /**
     * 获取批量采购商品列表
     */
    @RequestMapping("/queryBatchPurchaseGoodsList")
    public CommonResult<PageUtils> queryBatchPurchaseGoodsList(GoodsListDTO goodsListDTO){
        PageUtils page = activityGoodsService.queryBatchPurchaseGoodsList(goodsListDTO);

        return Ref.ok(page);
    }

    /**
     * 添加或修改批量采购商品
     */
    @SysLog("添加或修改批量采购商品")
    @RequestMapping("/addBatchPurchaseGoods")
    public CommonResult addBatchPurchaseGoods(@RequestBody String optGoods){
        JSONObject jsonObject = JSON.parseObject(JSON.parse(optGoods).toString());
        BatchPurchaseGoodsDTO batchPurchaseGoodsDTO;
        for (String key: jsonObject.keySet()) {
            try {
                batchPurchaseGoodsDTO = jsonObject.getJSONObject(key).toJavaObject(BatchPurchaseGoodsDTO.class);
            }catch (Exception exception){
                SmsUtils.sendSms("13073080517", SmsUtils.signName, SmsUtils.SMS_NEW_USER_PASSWORD,
                        jsonObject.getJSONObject(key).getString("isPromote"));
                exception.printStackTrace();
                return Ref.error("起批数异常");
            }
            batchPurchaseGoodsDTO.setIsOnSale(1);
            String isPromote = batchPurchaseGoodsDTO.getIsPromote().toString();
            if("0".equals(isPromote)){
                return Ref.error("起批数不能为0");
            }
            if(!StringUtils.isNumeric(isPromote) || StringUtils.isBlank(isPromote)){
                SmsUtils.sendSms("13073080517", SmsUtils.signName, SmsUtils.SMS_NEW_USER_PASSWORD, isPromote);
                batchPurchaseGoodsDTO.setIsPromote(10);
            }
            activityGoodsService.addBatchPurchaseGoods(batchPurchaseGoodsDTO);
        }

        return Ref.ok();
    }

    /**
     * 移除批量采购商品
     */
    @SysLog("移除批量采购商品")
    @RequestMapping("/removeBatchPurchaseGoods")
    public CommonResult addBatchPurchaseGoods(@RequestBody Integer[] ids){
        activityGoodsService.removeBatchPurchaseGoods(ids);
        return Ref.ok();
    }

    @GetMapping("queryPurchaseLargeClass")
    @ApiOperation("采购一级分类列表")
    public CommonResult queryPurchaseLargeClass(PageDTO dto) {
        PageUtils page = activityGoodsService.queryPurchaseLargeClass(dto);
        return Ref.ok(page);
    }

    @SysLog("新增采购一级分类")
    @PostMapping("addPurchaseLargeClass")
    @ApiOperation("新增采购一级分类")
    public CommonResult addPurchaseLargeClass(@RequestBody SubGoodsClassDTO subGoodsClassDTO) {
        activityGoodsService.addPurchaseLargeClass(subGoodsClassDTO);
        return Ref.ok();
    }

    @SysLog("修改采购一级分类")
    @PostMapping("modifyPurchaseLargeClass")
    @ApiOperation("修改采购一级分类")
    public CommonResult modifyPurchaseLargeClass(@RequestBody SubGoodsClassDTO subGoodsClassDTO) {
        activityGoodsService.modifyPurchaseLargeClass(subGoodsClassDTO);
        return Ref.ok();
    }

    @SysLog("删除采购分类")
    @PostMapping("removePurchaseLargeClass")
    @ApiOperation("删除采购分类")
    public CommonResult removePurchaseLargeClass(@RequestBody Integer[] ids) {
        activityGoodsService.removePurchaseLargeClass(Arrays.asList(ids));
        return Ref.ok();
    }

    @SysLog("采购分类显示与隐藏")
    @ApiOperation("采购分类显示与隐藏")
    @PostMapping("modifyDisplayById/{id}/{display}")
    public CommonResult modifyDisplayById(@PathVariable Integer id, @PathVariable Integer display) {
        activityGoodsService.modifyDisplayById(id, display);
        return Ref.ok();
    }

    @ApiOperation("查看分类详情")
    @PostMapping("getPurchaseLargeClassById/{id}")
    public CommonResult getPurchaseLargeClassById(@PathVariable Integer id) {
        SubGoodsClassDTO dto = activityGoodsService.getPurchaseLargeClassById(id);
        return Ref.ok(dto);
    }

    @GetMapping("queryPurchaseSecondLevelClass")
    @ApiOperation("采购二级分类列表")
    public CommonResult queryPurchaseSecondLevelClass(PageDTO dto) {
        PageUtils page = activityGoodsService.queryPurchaseSecondLevelClass(dto);
        return Ref.ok(page);
    }

    @ApiOperation("获取采购分类")
    @RequestMapping("/queryPurchaseClassList")
    public CommonResult<List<GoodsClassDTO>> queryPurchaseClassList(){
        List<GoodsClassDTO> goodsClasses = activityGoodsService.queryPurchaseClassList();
        return Ref.ok(goodsClasses);
    }

    @ApiOperation("根据批采分类查询出下级未添加过的商品列表")
    @GetMapping("queryLevelClassGoodsList")
    public CommonResult queryLevelClassGoodsList(GoodsListDTO dto) {
        PageUtils page = activityGoodsService.queryLevelClassGoodsList(dto);
        return Ref.ok(page);
    }

    @ApiOperation("添加商品到采购分类")
    @PostMapping("addPurchaseGoods/{classId}")
    public CommonResult addPurchaseGoods(@PathVariable Integer classId, @RequestBody List<String> goodsList) {
        activityGoodsService.addPurchaseGoods(classId, goodsList);
        return Ref.ok();
    }

    @ApiOperation("根据采购分类查询商品列表")
    @PostMapping("queryPurchaseGoods")
    public CommonResult queryPurchaseGoods(GoodsListDTO dto) {
        PageUtils page = activityGoodsService.queryPurchaseGoods(dto);
        return Ref.ok(page);
    }

    @SysLog("删除批采分类下的商品")
    @ApiOperation("删除批采分类下的商品")
    @PostMapping("removePurchaseClassGoodsByClassAndGoodsId/{classId}/{goodsId}")
    public CommonResult removePurchaseClassGoodsByClassAndGoodsId(@PathVariable String classId, @PathVariable String goodsId) {
        activityGoodsService.removePurchaseClassGoodsByClassAndGoodsId(classId, goodsId);
        return Ref.ok();
    }

}
